﻿using System.Text.RegularExpressions;

namespace CommonEngine
{
    /// <summary> 
    /// 正则表达式 
    /// </summary> 
    public class CxRegex
    {
        /// <summary>
        /// 验证Email地址 
        /// </summary>
        public static bool IsValidEmail(string strIn)
        {
            return Regex.IsMatch(strIn, "^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");
        }

        /// <summary>
        /// 验证是否为小数
        /// </summary>
        public static bool IsValidDecimal(string strIn)
        {
            return Regex.IsMatch(strIn, "^[0]{0,1}[.]{1}[0-9]{1,}$");
        }

        /// <summary>
        /// 验证两位小数
        /// </summary>
        public static bool Is2Decimal(string str_decimal)
        {
            return Regex.IsMatch(str_decimal, @"^[0-9]*[.]{1}[0-9]{2}$");
        }

        /// <summary>
        /// 验证一年的12个月
        /// </summary>
        public static bool IsMonth(string str_Month)
        {
            return Regex.IsMatch(str_Month, @"^(0?[[1-9]|1[0-2])$");
        }

        /// <summary>
        /// 验证一个月的31天
        /// </summary>
        public static bool IsDay(string str_day)
        {
            return Regex.IsMatch(str_day, @"^((0?[1-9])|((1|2)[0-9])|30|31)$");
        }

        /// <summary>
        /// 验证是否为电话号码 
        /// </summary>
        public static bool IsValidTel(string strIn)
        {
            return Regex.IsMatch(strIn, @"(/d+-)?(/d{4}-?/d{7}|/d{3}-?/d{8}|^/d{7,8})(-/d+)?");
        }

        /// <summary>
        /// 验证后缀名 
        /// </summary>
        public static bool IsValidPostfix(string strIn)
        {
            return Regex.IsMatch(strIn, @"/.(?i:gif|jpg)$");
        }

        /// <summary>
        /// 验证字符是否在4至12之间 
        /// </summary>
        public static bool IsValidByte(string strIn)
        {
            return Regex.IsMatch(strIn, @"^[a-z]{4,12}$");
        }

        /// <summary>
        /// 验证IP 
        /// </summary>
        public static bool IsValidIp(string strIn)
        {
            return Regex.IsMatch(strIn, @"^(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])$");
        }

        /// <summary>
        /// 验证输入字符串 (至少8个字符)
        /// </summary>
        public static bool IsLength(string str_Length)
        {
            return Regex.IsMatch(str_Length, "^.{8,}$");
        }

        /// <summary>
        /// 验证数字输入
        /// </summary>
        public static bool IsNumber(string str_number)
        {
            return Regex.IsMatch(str_number, "^[0-9]+$");
        }

        /// <summary>
        /// 验证整数
        /// </summary>
        public static bool IsInteger(string str_number)
        {
            return Regex.IsMatch(str_number, "^[-,+]?[0-9]+$");
        }

        /// <summary>
        /// 验证手机
        /// </summary>
        public static bool IsCellphoneNum(string str_number)
        {
            return Regex.IsMatch(str_number, "^1[3-9][0-9]{9}$");
        }

        /// <summary>
        /// 验证密码长度 (6-18位)
        /// </summary>
        public static bool IsPasswLength(string str_Length)
        {
            return Regex.IsMatch(str_Length, "^/d{6,18}$");
        }
    }
}
